CSRC =
CPPSRC =
ASMSRC =

# RTOS_DIR = CoOS
#RTOS_INCLUDE_DIR =$(RTOS_DIR) $(RTOS_DIR)/kernel $(RTOS_DIR)/portable $(RTOS_DIR)/portable/GCC


#CSRC +=$(wildcard $(COOS_DIR)/*.c)
#CSRC +=$(wildcard $(COOS_DIR)/*/*.c)
#CSRC +=$(wildcard $(COOS_DIR)/*/*/*.c)

RTOS_DIR =FreeRTOS
RTOS_INCLUDE_DIR =$(RTOS_DIR) $(RTOS_DIR)/include $(RTOS_DIR)/portable/gcc

CSRC +=$(wildcard $(RTOS_DIR)/*.c)
CSRC +=$(wildcard $(RTOS_DIR)/*/*.c)
CSRC +=$(wildcard $(RTOS_DIR)/*/*/*.c)

DRIVER_DIR =driver
DRIVER_INCLUDE_DIR =$(DRIVER_DIR)
CSRC +=$(wildcard $(DRIVER_DIR)/*.c)
CSRC +=$(wildcard $(DRIVER_DIR)/*/*.c)


FRGUI_DIR =frgui
FRGUI_INCLUDE_DIR =$(FRGUI_DIR) $(FRGUI_DIR)/core $(FRGUI_DIR)/font $(FRGUI_DIR)/widget $(FRGUI_DIR)/window $(FRGUI_DIR)/gdi $(FRGUI_DIR)/port
CSRC +=$(wildcard $(FRGUI_DIR)/*.c)
CSRC +=$(wildcard $(FRGUI_DIR)/*/*.c)
CPPSRC +=$(wildcard $(FRGUI_DIR)/*.cpp)
CPPSRC +=$(wildcard $(FRGUI_DIR)/*/*.cpp)


FATFS_DIR =FatFs
FATFS_INCLUDE_DIR =$(FATFS_DIR) $(FATFS_DIR)/Port
CSRC +=$(wildcard $(FATFS_DIR)/*.c) $(wildcard $(FATFS_DIR)/*/*.c)
CPPSRC +=$(wildcard $(FATFS_DIR)/*/*.cpp) $(wildcard $(FATFS_DIR)/*/*.cpp)

EEFS_DIR =eefs
EEFS_INCLUDE_DIR =$(EEFS_DIR) $(EEFS_DIR)/port
CSRC +=$(wildcard $(EEFS_DIR)/*.c) $(wildcard $(EEFS_DIR)/*/*.c)
CPPSRC +=$(wildcard $(EEFS_DIR)/*.cpp) $(wildcard $(EEFS_DIR)/*/*.cpp)


FWLIB_DIR =STM32F2xx_StdPeriph_Lib_V1.1.0/Libraries
FWLIB_CMSIS_INCLUDE_DIR =$(FWLIB_DIR)/CMSIS/Include/ $(FWLIB_DIR)/CMSIS/Device/ST/STM32F2xx/Include
FWLIB_DRIVER_INCLUDE_DIR =$(FWLIB_DIR)/STM32F2xx_StdPeriph_Driver/inc
FWLIB_INCLUDE_DIR =$(FWLIB_CMSIS_INCLUDE_DIR) $(FWLIB_DRIVER_INCLUDE_DIR)

ASMSRC +=$(wildcard $(FWLIB_DIR)/*/*/*/*/*/*/*/*.s)
CSRC +=$(wildcard $(FWLIB_DIR)/*/*/*.c)

APP_DIR =app
APP_INCLUDE_DIR =$(APP_DIR) $(APP_DIR)/port $(APP_DIR)/GUI $(APP_DIR)/GUI/bitmaps

CSRC +=$(wildcard $(APP_DIR)/*.c $(APP_DIR)/*/*.c $(APP_DIR)/*/*/*.c)
CPPSRC +=$(wildcard $(APP_DIR)/*.cpp $(APP_DIR)/*/*.cpp)

INCLUDE_DIR =$(RTOS_INCLUDE_DIR)
INCLUDE_DIR += $(DRIVER_INCLUDE_DIR)
INCLUDE_DIR += $(FRGUI_INCLUDE_DIR)
INCLUDE_DIR += $(FATFS_INCLUDE_DIR)
INCLUDE_DIR += $(EEFS_INCLUDE_DIR)
INCLUDE_DIR += $(FWLIB_INCLUDE_DIR)
INCLUDE_DIR += $(APP_INCLUDE_DIR)

#$(shell echo $(CSRC) >test.txt)

CROSS_PLATFORM :=arm-none-eabi-
TARGET =freetx

CC =$(CROSS_PLATFORM)gcc
CPP=$(CROSS_PLATFORM)g++
AS =$(CROSS_PLATFORM)as
LD =$(CROSS_PLATFORM)ld
OBJCOPY =$(CROSS_PLATFORM)objcopy
OBJDUMP =$(CROSS_PLATFORM)objdump
SIZE =$(CROSS_PLATFORM)size
NM =$(CROSS_PLATFORM)nm

CFG_DEBUG =  #-DDEBUG_FRGUI -DDEBUG_DC

CFLAGS += -c -nostartfiles -mcpu=cortex-m3 $(CFG_DEBUG) -O0 -gdwarf-2 -mthumb -Wall -T $(APP_DIR)/stm32_flash.ld
CFLAGS +=$(foreach n,$(INCLUDE_DIR), -I$(n))

CPPFLAGS = -c -nostartfiles -mcpu=cortex-m3 $(CFG_DEBUG) -O0 -gdwarf-2 -mthumb -Wall -T $(APP_DIR)/stm32_flash.ld
CPPFLAGS += -fno-rtti
#CPPFLAGS += -D__cplusplus
CPPFLAGS += -fno-exceptions
CPPFLAGS +=$(foreach n,$(INCLUDE_DIR), -I$(n))

#$(shell echo $(INCLUDE_DIR) >test.txt)

.PHONY: all clean cppfiles cfiles sfiles

C_OBJS =$(CSRC:.c=.o)
ASM_OBJS =$(ASMSRC:.s=.o)
CPP_OBJS =$(CPPSRC:.cpp=.o)

LDSCRIPT =-T $(APP_DIR)/stm32_flash.ld
#LDSCRIPT += --specs=rdimon.specs -lrdimon
LDSCRIPT += -lc -lm -lgcc
#LDSCRIPT +=  -nostdlib
LDSCRIPT += -mcpu=cortex-m3 -mthumb
#-Xlinker
#LDSCRIPT += -fno-rtti
LDSCRIPT += -lstdc++
LDSCRIPT += -Wl,--gc-sections
LDSCRIPT += -Wl,-Map=$(TARGET).map
#LDSCRIPT += c:/progra~1/codeso~1/source~1/lib/gcc/arm-none-eabi/4.5.2/libgcc.a

.PHONY: all clean cppfiles cfiles sfiles

all: $(CPP_OBJS) $(C_OBJS) $(ASM_OBJS)
	@echo --------linking---------
	$(CPP) $(LDSCRIPT) -o $(TARGET).elf $^
	@echo --------objcopy---------
	$(OBJCOPY) -Obinary $(TARGET).elf $(TARGET).bin

%.o: %.c
	$(CC) $(CFLAGS) $< -o $@
	@echo -------------file $< successfully------------------

%.o: %.s
	$(AS) $< -o $@
	@echo -------------file $< successfully------------------

%.o: %.cpp
	$(CPP) $(CPPFLAGS) $< -o $@
	@echo -------------file $< successfully------------------

cpp: $(CPP_OBJS)
	@echo ----------------------done---------------------------
cfiles: $(C_OBJS)
	@echo ----------------------done---------------------------
sfiles: $(ASM_OBJS)
	@echo ----------------------done---------------------------
    
clean:
	rm -rf *.o
	rm -rf */*.o
	rm -rf */*/*.o
	rm -rf */*/*/*.o
	rm -rf */*/*/*/*.o
	rm -rf */*/*/*/*/*.o
	rm -rf */*/*/*/*/*/*.o
	rm -rf */*/*/*/*/*/*/*.o
	rm -rf */*/*/*/*/*/*/*/*.o
	rm -rf */*/*/*/*/*/*/*/*/*.o
	rm -rf freetx.elf
	rm -rf freetx.bin
	rm -rf freetx.map

    
    
